<template>
    <div class="orderdetailsForm">
      <!-- element组件库的Dialog对话框默认可以通过点击 modal 关闭 Dialog，即点击空白处弹框可关闭。 -->
      <!-- :visible.sync 子组键改变父组件的值 控制子组件的显示-->
      <el-dialog title="修改订单详情" :visible.sync="editDialogIsShow" :before-close="handleClose" :close-on-click-modal="false">
      <el-form :model="orderdetails" label-width="80px"  :rules="editOrderdetailsRules" ref="editOrderdetailsForm">
        <el-form-item label="订单名称" prop="odtitle">
          <el-col :span="16">
           <el-input v-model="orderdetails.odtitle" placeholder="请填入订单名称" clearable/>
          </el-col>
        </el-form-item>
        <el-form-item label="订单数量" prop="odnumber">
        <el-col :span="16">
          <el-input v-model="orderdetails.odnumber" placeholder="请填入订单数量" clearable/>
        </el-col>
      </el-form-item>
        <el-form-item label="订单详情单价" prop="odprice">
        <el-col :span="16">
          <el-input v-model="orderdetails.odprice" placeholder="请填入单价" clearable/>
        </el-col>
      </el-form-item>
        <el-form-item label="订单详情" prop="oddescribe">
          <el-col :span="16">
            <el-input type="textarea" :rows="5" v-model="orderdetails.oddescribe" placeholder="请输入订单详情内容" />
          </el-col>
        </el-form-item>
      </el-form>
      <el-row>
         <el-col :span="12">
         <span>&nbsp;</span></el-col>
        <el-col :span="6">
           <span><el-button type="primary" @click="editOrderdetails('editOrderdetailsForm')">修改</el-button></span>
        </el-col>

    </el-row>
      </el-dialog>
    </div>
</template>

<script>
  import api from "../../../../axios/api"
  import rules from "../../../../static/js/validator/rules.js"
    var initfavOption = [];
    export default {
        name: "editOrderdetails",
        data(){
          return {orderdetails:{odtitle:"",odnumber:"",odprice:"",oddescribe:""},
              checkAll: false,
              isIndeterminate: true,
              editOrderdetailsRules:{
                odtitle:[{required:true,message:"订单名称不能为空",trigger:'blur'}],
                odnumber:[{required:true,message:"订单数量不能为空",trigger:'blur'},{trigger:'change',validator:rules.FormValidate.Form().validateNumber}],
                odprice:[{required:true,message:"订单数量不能为空",trigger:'blur'},{trigger:'change',validator:rules.FormValidate.Form().validateNumber}],
                oddescribe:[{required:true,message:"订单详情不能为空",trigger:'blur'}]},
              editDialogIsShow:false

          }
        },
      methods: {
        init(orderdetails) {
          this.editDialogIsShow = true;
          this.orderdetails = orderdetails;
          console.log(orderdetails);
        },
        handleCheckAllChange(val) {
          this.orderdetails.favs = val ? initfavOption : [];
          this.isIndeterminate = false;
        },
        handleCheckedFavsChange(value) {
          let checkedCount = value.length;
          this.checkAll = checkedCount === this.favs.length;
          this.isIndeterminate = checkedCount > 0 && checkedCount < this.favs.length;
        },
        //关闭页面
        handleClose() {
          this.editDialogIsShow = false;
          //给父组件传参
          this.$emit("editDialogIsShow", this.editDialogIsShow);
        },
        editOrderdetails:function(editOrderdetailsForm){
          this.$refs[editOrderdetailsForm].validate(valid => {//此处refs对象和标签上的ref对应
            if (valid) {
              //如果通过验证 to do...
              console.log(this.orderdetails);
              alert(this.orderdetails.nname+"修改成功");
              this.setDataFromAxios();
              this.handleClose();
            } else {
              console.log('error submit!!')
              return false
            }
          });

        },
        //关闭页面
        handleClose() {
          this.editDialogIsShow = false;
          //给父组件传参
          this.$emit("editDialogIsShow", this.editDialogIsShow);
        },
        setDataFromAxios:function(){
          //为什么要封装一下方法 1.每个vue组件有自己的一些参数处理，简化调用本来这个setDataFromAxios这个方法属于api 重新封装就属于当前页面
          api.setDataFromAxios("/api/orderdetailsController/updateOrderdetailsByOdno",this.orderdetails,"post")
            .then(res=>{this.leagues = res.leagues});//.then方法 就是当服务器有返回的时候执行回调函数，对比jquery的ajax success:function（data）
        },
        updateView(e) {
          this.$forceUpdate();
        }

      },
      //监听
      watch: {
        editDialogIsShow(newValue, oldValue) {//配合:visible.sync监听变化
          this.editDialogIsShow = newValue;
        }
      },
      created() {
        for(var favindex=0;favindex < favOptions.length;favindex++){
          initfavOption.push(favOptions[favindex].name);
        }
      }
    }
</script>
<style scoped lang="stylus">
 .orderdetailsForm
   margin 0 auto
   width 1000px
   text-align left
</style>
